package cn.lonecloud.randomCall;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import cn.lonecloud.Common;
import cn.lonecloud.randomCall.help.RandomCallHelp;
import cn.lonecloud.randomCall.model.Student;
import cn.lonecloud.util.FileUtil;
import javax.swing.JList;

public class RandomCall extends JFrame {

	private JPanel contentPane;
	private JTextField textField_studentNum;
	private JTextField textField_studentName;
	private JTextField filePath;
	private RandomCallHelp callHelp;
	Student student;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RandomCall frame = new RandomCall();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	/**
		设置随机数字
	 */
	public void setRandomValue(){
		student = callHelp.RandomExcel();
		if (student.getStudentName().equals("点名完成")) {
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(null, "点名完成", "点名完成",JOptionPane.WARNING_MESSAGE);
			return;
		}
		textField_studentName.setText(student.getStudentName());
		textField_studentNum.setText(student.getStudentNum());
	}
	/**
	 * Create the frame.
	 */
	public RandomCall() {
		setTitle("随机点名器");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		textField_studentNum = new JTextField();
		textField_studentNum.setColumns(10);

		JLabel label_studentNum = new JLabel("学号：");

		textField_studentName = new JTextField();
		textField_studentName.setColumns(10);

		JLabel label_studentName = new JLabel("姓名：");
		filePath = new JTextField();
		filePath.setColumns(10);
		JButton btn_selectFile = new JButton("选择文件");
		btn_selectFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TDD 选择文件
				JFileChooser jfc = new JFileChooser();
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				jfc.showDialog(new JLabel(), "选择");
				File file = jfc.getSelectedFile();
				if (file != null) {
					try {
						String type = FileUtil.getFileType(file);
						if (Common.OFFICE_EXCEL_2003_TYPE.equals(type)
								|| Common.OFFICE_EXCEL_2010_TYPE.equals(type)) {
							filePath.setText(file.getAbsolutePath());
							try {
								callHelp=new RandomCallHelp(file);
								setRandomValue();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						}else{
							Toolkit.getDefaultToolkit().beep();
							JOptionPane.showMessageDialog(null, "选择文件错误", "必须选择Excel文件",JOptionPane.ERROR_MESSAGE);
						}
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		
		JButton button_1 = new JButton("到了");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				student.setStatus(0);
				try {
					callHelp.setstatus(student);
					setRandomValue();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		JButton button_2 = new JButton("没到");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				student.setStatus(1);
				try {
					callHelp.setstatus(student);
					setRandomValue();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		JButton button = new JButton("请假");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				student.setStatus(2);
				try {
					callHelp.setstatus(student);
					setRandomValue();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		JButton button_3 = new JButton("保存");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					callHelp.save();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(35)
					.addComponent(filePath, GroupLayout.PREFERRED_SIZE, 282, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btn_selectFile)
					.addContainerGap(21, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(label_studentName, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
							.addGap(12)
							.addComponent(textField_studentName))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(label_studentNum)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(textField_studentNum, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(button_3)
						.addComponent(button_2)
						.addComponent(button_1)
						.addComponent(button, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
					.addGap(32))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(filePath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btn_selectFile))
					.addGap(50)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField_studentNum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_studentNum)
						.addComponent(button_1))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
							.addComponent(label_studentName)
							.addGap(79))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(12)
							.addComponent(button_2)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(12)
									.addComponent(textField_studentName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(button)))
							.addGap(36)
							.addComponent(button_3)
							.addContainerGap())))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
